/* Scheduler include files. */
#include <stdlib.h>
#include "FreeRTOS.h"
#include "task.h"
#include "uart.h"

/* Demo program include files. */
#include "serial.h"
//#include "comtest.h"
//#include "partest.h"

#define comSTACK_SIZE				configMINIMAL_STACK_SIZE
#define comTX_LED_OFFSET			( 0 )
#define comRX_LED_OFFSET			( 1 )
#define comTOTAL_PERMISSIBLE_ERRORS ( 2 )

/* The Tx task will transmit the sequence of characters at a pseudo random
interval.  This is the maximum and minimum block time between sends. */
#define comHEART_WAIT			( ( portTickType ) 3 )
#define comWAIT_TIME			( ( portTickType ) 200/portTICK_RATE_MS )
#define comWAIT_TIME_2			( ( portTickType ) 1000/portTICK_RATE_MS )
#define NO_BLOCK_TIME			( ( portTickType ) 0 )

/* The transmit task as described at the top of the file. */
static portTASK_FUNCTION_PROTO( vtestB1, pvParameters );

/* The receive task as described at the top of the file. */
static portTASK_FUNCTION_PROTO( vtestB2, pvParameters );

/* The LED that should be toggled by the Rx and Tx tasks.  The Rx task will
toggle LED ( uxBaseLED + comRX_LED_OFFSET).  The Tx task will toggle LED
( uxBaseLED + comTX_LED_OFFSET ). */
static unsigned portBASE_TYPE uxBaseLED = 0;
static xTaskHandle heartBeat = NULL;
static xTaskHandle heartListen = NULL;
static xComPortHandle xPort = NULL;
static portBASE_TYPE xErrorOccurred = pdFALSE;
//static portBASE_TYPE xHBReceived = pdFALSE;
int beatCount = 0;
int beatSuccess = 0;

/*-----------------------------------------------------------*/

void vStartTestTasks( unsigned portBASE_TYPE uxPriority, unsigned portBASE_TYPE uxLED )
{
	/* Initialise the com port then spawn the Rx and Tx tasks. */
	uxBaseLED = uxLED;

	/* The Tx task is spawned with a lower priority than the Rx task. */
	xTaskCreate( vtestB1, ( signed char * ) "testB1", comSTACK_SIZE, NULL, uxPriority, &heartBeat );
	xTaskCreate( vtestB2, ( signed char * ) "testB2", comSTACK_SIZE, NULL, uxPriority, &heartListen );
}
/*-----------------------------------------------------------*/

static portTASK_FUNCTION( vtestB1, pvParameters )
{
  portTickType xTimeToWait = comWAIT_TIME;
  signed char constHeartBeat[2];
	/* Just to stop compiler warnings. */
	( void ) pvParameters;

	for( ;; )
	{
                /*constHeartBeat[0] = 'Z';  // heartbeat
                constHeartBeat[1] = (char)beatCount;  // # of heartbeats since last one received
                //ccTX(constHeartBeat);
                beatCount++;*/
                P1OUT ^= 0x01;
		vTaskDelay( xTimeToWait );
	}
}
/*-----------------------------------------------------------*/

static portTASK_FUNCTION( vtestB2, pvParameters )
{
  portTickType xTimeToWait = comWAIT_TIME_2;
  signed char constHeartBeat[2];
	/* Just to stop compiler warnings. */
	( void ) pvParameters;

	for( ;; )
	{
                /*constHeartBeat[0] = 'Z';  // heartbeat
                constHeartBeat[1] = (char)beatCount;  // # of heartbeats since last one received
                //ccTX(constHeartBeat);
                beatCount++;*/
                P5OUT ^= 0xFF;
		vTaskDelay( xTimeToWait );
	}
} /*lint !e715 !e818 pvParameters is required for a task function even if it is not referenced. */
/*-----------------------------------------------------------*/

portBASE_TYPE xAreComTestTasksStillRunning( void )
{
  portBASE_TYPE xReturn;

  if( xErrorOccurred == pdTRUE )
  {
    xReturn = pdFALSE;        // didnt receive heartbeat in a while
    vTaskDelete(vtestB1);  // stop sending heartbeats
  }
  else
  {
    xReturn = pdTRUE;
  }

  return xReturn;
}

void xEndAllHeart( void ){
    vTaskDelete(vtestB1);  // stop sending heartbeats
    vTaskDelete(vtestB2);  // stop sending heartbeats
}
/*-----------------------------------------------------------*/
